package com.maoxx.humanresourcemanage_service.controller;

import com.maoxx.humanresourcemanage_service.dto.ResponseDTO;
import com.maoxx.humanresourcemanage_service.entity.Staff;
import com.maoxx.humanresourcemanage_service.service.StaffRoleService;
import com.maoxx.humanresourcemanage_service.service.StaffService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @className: StaffController
 * @author: Maoxx
 * @date: 2024/11/7 14:12
 * @Version: 1.0
 * @description:
 */

@RestController
@RequestMapping("/staff")
public class StaffController {

    @Resource
    private StaffService staffService;

    @Resource
    private StaffRoleService staffRoleService;

    @ApiOperation("新增")
    @PostMapping
    public ResponseDTO add(@RequestBody Staff staff){
        return staffService.add(staff);
    }

    @ApiOperation("编辑更新")
    @PutMapping
    public ResponseDTO edit(@RequestBody Staff staff){
        return staffService.edit(staff);
    }

    @ApiOperation("多条件分页查询")
    @PostMapping("/page")
    public ResponseDTO list(@RequestParam(defaultValue = "1") Integer current,
                            @RequestParam(defaultValue = "10") Integer size,
                            @RequestBody Staff staff){
        return this.staffService.list(current, size, staff);
    }


    /**
     * 根据id查询员工信息接口
     * @param id 员工id
     * @return ResponseDTO
     */
    @ApiOperation("查询")
    @GetMapping("/{id}")
    public ResponseDTO findById(@PathVariable("id") Integer id) {
        return staffService.findById(id);
    }

    @ApiOperation("逻辑删除")
    @DeleteMapping("/{id}")
    public ResponseDTO deleteOne(@PathVariable Integer id){
        return this.staffService.deleteOne(id);
    }

    @ApiOperation("批量逻辑删除")
    @DeleteMapping("/batch/{ids}")
    public ResponseDTO deleteBatch(@PathVariable List<Integer> ids){
        return this.staffService.deleteBatch(ids);
    }


    @ApiOperation("数据导出接口")
    @GetMapping("/export")
    public ResponseDTO export(HttpServletResponse response) throws IOException {
        return this.staffService.export(response);
    }

    @ApiOperation("数据导入接口")
    @PostMapping("/import")
    public ResponseDTO imp(MultipartFile file) throws IOException {
        return this.staffService.imp(file);
    }

    @ApiOperation("得到员工的角色")
    @GetMapping("/role/{staffId}")
    public ResponseDTO getRole(@PathVariable Integer staffId){
        return staffRoleService.getRole(staffId);
    }

    @ApiOperation("为员工设置角色")
    @PostMapping("/role/{staffId}")
    public ResponseDTO setRole(@PathVariable Integer staffId, @RequestBody List<Integer> roleIds){
        return this.staffRoleService.setRole(staffId, roleIds);
    }
}
